﻿using AntDesign;
using Microsoft.AspNetCore.Components;
using System.Collections.Generic;
using System.Threading.Tasks;
using YwhLib;
using YwhWeb.Models;
using YwhWeb.Services;

namespace YwhWeb.Pages.Daily
{
    public partial class UnitView
    {
        private CurrentUser _currentUser = new CurrentUser();

        [Inject] protected IUserService UserService { get; set; }

        private void HandleFinish()
        {
        }

        protected override async Task OnInitializedAsync()
        {
            await base.OnInitializedAsync();
            this.unitRsp = YwhCore.UnitService.List(_pageIndex, _pageSize);
            this.phaseOptions = YwhCore.PhaseService.List(1, 100).Items;
            _currentUser = await UserService.GetCurrentUserAsync();
        }

        public YwhLib.QueryRsp<YwhLib.Unit> unitRsp = new YwhLib.QueryRsp<YwhLib.Unit>();

        IEnumerable<YwhLib.Unit> selectedRows;
        ITable table;

        int _pageIndex = 1;
        int _pageSize = 10;
        int _total = 0;

        bool drawerVisible = false;

        void NewDrawer()
        {
            this.editUnit = new YwhLib.Unit();
            this.drawerVisible = true;
        }

        public async Task OnChange(AntDesign.TableModels.QueryModel<YwhLib.Unit> queryModel)
        {
            //Console.WriteLine(JsonSerializer.Serialize(queryModel));
        }

        private YwhLib.Unit editUnit = new YwhLib.Unit();

        Phase phaseSelect = null;
        Building buildingSelect = null;

        List<Phase> phaseOptions = new List<Phase>();

        List<Building> buildingOptions = new List<Building>();

        private void OnSelectedItemChangedHandler(Phase value)
        {
            phaseSelect = value;
            this.buildingOptions = YwhCore.BuildingService.List(1, 100, phaseSelect.PhaseNo).Items;
        }

        private async Task DeleteUnit()
        {
            YwhCore.UnitService.Delete(editUnit);
            this.unitRsp = YwhCore.UnitService.List(_pageIndex, _pageSize);
            base.StateHasChanged();
        }

        private async Task UpdateUnit()
        {
            this.drawerVisible = true;
            YwhCore.UnitService.Update(editUnit);
            this.unitRsp = YwhCore.UnitService.List(_pageIndex, _pageSize);
            base.StateHasChanged();
        }

        private async Task SaveUnit()
        {
            YwhCore.UnitService.Save(editUnit);
            this.drawerVisible = false;
            this.unitRsp = YwhCore.UnitService.List(_pageIndex, _pageSize);
            base.StateHasChanged();
        }
    }
}
